// <copyright file="RPGRoguelikeEngineTestApp.PluginLoad.cs" company="TenneySoftware">
// Copyright (c) 2009 All Right Reserved
// </copyright>
// <author>Robert Tenney</author>
// <email>rob10e@yahoo.com</email>
// <date>April 2009</date>
// <summary>Main application display form - plugin load portion.</summary>

// License:

// Product: RPGRoguelikeEngine .Net
// Author: Robert N. Tenney
// Copyright: April 2009

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation version 2
// of the License.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

// This license covers all source code, binaries, and other materials related to this project
// unless specifically stated otherwise.

using System;
using System.Windows.Forms;
using TenneySoftware.Engine;
using TenneySoftware.LevelGenerator;

namespace TenneySoftware.RPGRoguelikeEngineTestApp {
    /// <content>
    /// Main application display form - plugin load portion.
    /// </content>
    public partial class MainForm {
        /// <summary>
        /// Method that loads the graphics manager list.
        /// </summary>
        public void LoadManagers() {
            string[][] managers = RPGEngine.ListGraphicsManagers();
            string selected = string.Empty;
            this.graphicsManagersList = new ToolStripItem[managers.Length];
            int iter = 0;
            foreach (string[] manager in managers) {
                if (manager == null) {
                    continue;
                }
                
                ToolStripMenuItem item = new ToolStripMenuItem();
                item.Text = manager[0];
                item.Name = "menuGraphicManagersItem" + item.Text;
                item.Click += new EventHandler(this.Manager_Click);
                if (iter == 0) {
                    item.Checked = true;
                    item.CheckState = CheckState.Checked;
                    selected = item.Text;
                } else {
                    item.CheckState = CheckState.Unchecked;
                }
                
                this.graphicsManagersList[iter] = item;
                iter++;
            }
            
            this.ManagerSelect(selected);
            if (this.graphicsManagersList == null) {
                return;
            }
            
            this.menuGraphicManagers.DropDownItems.AddRange(this.graphicsManagersList);
        }

        /// <summary>
        /// Select the graphics manager.
        /// </summary>
        /// <param name="text">String of the manager.</param>
        private void ManagerSelect(string text) {
            string[] found = RPGEngine.FindGraphicsManager(text);
            if (found != null) {
                this.engine.LoadManager(found);
            }
        }

        /// <summary>
        /// Method that loads the level generator list.
        /// </summary>
        private void LoadGenerators() {
            string[][] generators = RPGEngine.ListGenerators();
            string selectedPred = string.Empty;
            string selectedGen = string.Empty;
            int iterUp = 0;
            bool tagged = false;
            foreach (string[] generator in generators) {
                GeneratorData[] predefines = RPGEngine.ListPredefines(generator);
                ToolStripMenuItem item = new ToolStripMenuItem();
                item.Text = generator[0];
                item.Name = "menuLevelGeneratorsItem" + item.Text;
                ToolStripItem[] pred = new ToolStripItem[predefines.Length];
                int iter = 0;
                
                foreach (GeneratorData predefine in predefines) {
                    ToolStripMenuItem predItem = new ToolStripMenuItem();
                    predItem.Text = predefine.Name;
                    predItem.Name = "menuLevelGeneratorsItemPred" + predItem.Text;
                    predItem.Click += new EventHandler(this.Generator_Click);
                    if (iter == 0 && !tagged) {
                        predItem.Checked = true;
                        predItem.CheckState = CheckState.Checked;
                        selectedPred = predItem.Text;
                        tagged = true;
                    } else {
                        predItem.CheckState = CheckState.Unchecked;
                    }
                    
                    pred[iter] = predItem;
                    iter++;
                }
                
                if (iterUp == 0) {
                    item.Checked = true;
                    item.CheckState = CheckState.Checked;
                    selectedGen = item.Text;
                } else {
                    item.CheckState = CheckState.Unchecked;
                }
                
                item.DropDownItems.AddRange(pred);
                this.menuLevelGenerators.DropDownItems.Add(item);
                iterUp++;
            }
            
            this.GeneratorSelect(selectedGen, selectedPred);
        }
        
        /// <summary>
        /// Select a level generator.
        /// </summary>
        /// <param name="name">String name of the level generator.</param>
        /// <param name="predefine">String name of the predefined to use.</param>
        private void GeneratorSelect(string name, string predefine) {
            string[] found = RPGEngine.FindGenerator(name);
            if (found != null) {
                this.engine.LoadGenerator(found);
                this.engine.LevelGenerator.UsePredefinedSettings(this.engine.FindPredefinedIndex(predefine));
            }
        }
    }
}
